#include "Frm02.h"

Frm02::Frm02(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm02::~Frm02() {
}

void Frm02::onInit() {

}

void Frm02::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- MButton : wMMButton1                 -----------------------------*/
	wMMButton1 = new STTButton(p_rt, this, "wMMButton1", 1);
	wMMButton1->setMainArg(0, 0, 100, 100, false);
	wMMButton1->setImg("1", "./images/arrow_34_100X100.gray.svg", "");
	wMMButton1->setImg("0", "./images/arrow_34_100X100.svg", "");
	wMMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Label : wMLabel7                     -----------------------------*/
	wMLabel7 = new STTLabel(p_rt, wMMButton1, "wMLabel7");
	wMLabel7->setMainArg(9, 74, 81, 26, "查询语句", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel7->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel7->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel7->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- SqliteTable : wMSqliteTable1         -----------------------------*/
	wMSqliteTable1 = new STTTable(p_rt, this, "wMSqliteTable1");
	wMSqliteTable1->setMainArg(0, 172, 1024, 427, "1,c1,98;2,c2,98;3,c3,97", 7, false, 6);
	wMSqliteTable1->setHeadCellStyle(0xf0f0f0, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 16, "文泉驿微米黑", 0);
	wMSqliteTable1->setSelCellStyle(0x1a2b3, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 16, "文泉驿微米黑", 0);
	wMSqliteTable1->setDataCellStyle(0xffffff, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 16, "文泉驿微米黑", 0);
	tv_wMSqliteTable1 = new TTableView("tv_wMSqliteTable1", "S_CFG", "1", "1", nullptr, "", false);
	wMSqliteTable1->bindData(tv_wMSqliteTable1);

	/*----------------------------- Text : wMText1                       -----------------------------*/
	wMText1 = new STTTextArea(p_rt, this, "wMText1");
	wMText1->setMainArg(99, 0, 183, 45, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText1->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText1->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMText1->setFont(0x0, 16, "文泉驿微米黑", 0);
	wMText1->setText("192.168.1.101");

	/*----------------------------- Text : wMText2                       -----------------------------*/
	wMText2 = new STTTextArea(p_rt, this, "wMText2");
	wMText2->setMainArg(281, 0, 75, 45, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText2->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText2->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMText2->setFont(0x0, 16, "文泉驿微米黑", 0);
	wMText2->setText("1433");

	/*----------------------------- Text : wMText3                       -----------------------------*/
	wMText3 = new STTTextArea(p_rt, this, "wMText3");
	wMText3->setMainArg(355, 0, 75, 45, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText3->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText3->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::LC);
	wMText3->setFont(0x0, 16, "文泉驿微米黑", 0);
	wMText3->setText("sa");

	/*----------------------------- Text : wMText4                       -----------------------------*/
	wMText4 = new STTTextArea(p_rt, this, "wMText4");
	wMText4->setMainArg(429, 0, 104, 45, true, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText4->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText4->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::LC);
	wMText4->setFont(0x0, 16, "文泉驿微米黑", 0);
	wMText4->setText("123456");

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(630, 1, 98, 43, false);
	wMButton1->setPressedArg("连接", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("连接", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton1_clk_cb);

	/*----------------------------- Text : wMText5                       -----------------------------*/
	wMText5 = new STTTextArea(p_rt, this, "wMText5");
	wMText5->setMainArg(532, 0, 98, 45, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText5->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText5->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::LC);
	wMText5->setFont(0x0, 16, "文泉驿微米黑", 0);
	wMText5->setText("master");

	/*----------------------------- Label : wMLabel1                     -----------------------------*/
	wMLabel1 = new STTLabel(p_rt, this, "wMLabel1");
	wMLabel1->setMainArg(99, 44, 81, 26, "ip", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel1->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel1->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, this, "wMLabel2");
	wMLabel2->setMainArg(281, 44, 52, 26, "port", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(355, 44, 52, 26, "user", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel4                     -----------------------------*/
	wMLabel4 = new STTLabel(p_rt, this, "wMLabel4");
	wMLabel4->setMainArg(429, 44, 87, 26, "password", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel4->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel4->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel4->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel5                     -----------------------------*/
	wMLabel5 = new STTLabel(p_rt, this, "wMLabel5");
	wMLabel5->setMainArg(537, 44, 87, 26, "数据库名", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel5->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel5->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel5->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel6                     -----------------------------*/
	wMLabel6 = new STTLabel(p_rt, this, "wMLabel6");
	wMLabel6->setMainArg(746, 0, 258, 54, "", TextAlign::LEFT, LabelLongMode::WRAP, 30);
	wMLabel6->setFonts(0xff0000, 16, "文泉驿微米黑", 0);
	wMLabel6->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel6->setFormatText(TextAlign::LEFT, LabelLongMode::WRAP, 30);
	wMLabel6->bindData(_V("varLast"));

	/*----------------------------- Text : wMText6                       -----------------------------*/
	wMText6 = new STTTextArea(p_rt, this, "wMText6");
	wMText6->setMainArg(99, 69, 812, 40, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText6->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText6->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMText6->setFont(0x0, 12, "文泉驿微米黑", 0);
	wMText6->setText("SELECT optname as c1, value as c2, major_version as c3  FROM MSreplication_options");

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(925, 70, 98, 43, false);
	wMButton2->setPressedArg("查询", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("查询", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton2_clk_cb);

	/*----------------------------- Text : wMText7                       -----------------------------*/
	wMText7 = new STTTextArea(p_rt, this, "wMText7");
	wMText7->setMainArg(99, 118, 812, 40, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText7->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 9, 4, 4, 255);
	wMText7->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMText7->setFont(0x0, 12, "文泉驿微米黑", 0);
	wMText7->setText("update MSreplication_options set major_version=major_version+1 where optname='merge'");

	/*----------------------------- Label : wMLabel8                     -----------------------------*/
	wMLabel8 = new STTLabel(p_rt, this, "wMLabel8");
	wMLabel8->setMainArg(9, 125, 81, 26, "执行语句", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel8->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel8->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel8->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(924, 116, 98, 43, false);
	wMButton3->setPressedArg("执行", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->setReleasedArg("执行", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton3_clk_cb);

	/*called after widget created.*/

	return; //createParts END
}

void Frm02::onLoad() {
#if __arm__
	string result;
	Util::exeSync("mv /customer/prm/odbcinst.ini /etc/", "r", result);
	Util::exeSync("sync", "r", result);
#endif
}

bool Frm02::onClosing() {
	return true;
}

void Frm02::onDispose() {

}
#include "util/StringUtil.h"
#include "db/IDataBase.h"
using namespace stt::db;
IDatabase * psqlserver = NULL;

void Frm02::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（连接）的点击事件*/
	string ip = wMText1->getText();
	string strport = wMText2->getText();
	int port = StringUtil::safeStoi(strport);
	string uid = wMText3->getText();
	string pwd = wMText4->getText();
	string dbname = wMText5->getText();
	string dbid = ip+strport+uid+pwd+dbname;
	IDatabase* ptmp = DBMgr::getdb(dbid, DBTYPE::MSSQL, ip, port, uid, pwd, dbname);
	cout<<dbid<<endl;
	if(ptmp) {
		psqlserver = ptmp;
		vm->setChar("varLast", "connect ok");
	} else {
		vm->setChar("varLast", "connect fail");
	}
}

void Frm02::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（查询）的点击事件*/
	//1.把这句复制下来，修改一下：重新设定字段及显示的标题:c1,c2,c3是查询结果的中字段名；“第1列，第2列，第3列”为表头
	wMSqliteTable1->setMainArg(0, 172, 1024, 427, "c1,第1列,98;c2,第2列,98;c3,第3列,97", 7, false, 6);
	//2.设定数据源连接
	tv_wMSqliteTable1->resetDB(psqlserver);
	//3.查询语句，这个sql的结果将作为refine的表
	string sql = wMText6->getText();
	string str = "(" + sql +") t1";
	//4.设置排序字段(如不设置为第1个字段)
	tv_wMSqliteTable1->m_key = "c1";
	//5.设置是否倒序
	tv_wMSqliteTable1->enDesc(true);
	//6.执行"select * from " + str
	tv_wMSqliteTable1->refine(str);
	//7.更新表格控件
	tv_wMSqliteTable1->drawData(true);
}

void Frm02::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton3（执行）的点击事件*/
	string sql = wMText7->getText();
	psqlserver->execute(sql);
}
